//
//  SearchAlbumViewController.m
//  HuiYinMaster
//
//  Created by Icloud-Zhanyong on 2017/4/19.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "SearchAlbumViewController.h"
#import "AFNetWorking.h"
#import "SeverManager.h"
#import "UIImageView+WebCache.h"
#import "SDWebImageManager.h"
#import "AlbumListModel.h"
#import "AlbumListTableViewCell.h"
#import "AlbumDetailViewController.h"

@interface SearchAlbumViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
{
@private NSString * searchText;
@private UITextField * SearchTextFiled;
}
@property(nonatomic,strong)NSMutableArray<AlbumListModel *> * albumListDataArray;
@end

@implementation SearchAlbumViewController

- (void)viewDidLoad {
    self.albumListDataArray = [NSMutableArray array];
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton * searchBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [searchBtn addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setTitle:@"搜索" forState:normal];
    [searchBtn sizeToFit];
    UIBarButtonItem * searchItem = [[UIBarButtonItem alloc]initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = searchItem;
    SearchTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * (0.8), SCREEN_HEIGHT * (0.04))];
    SearchTextFiled.layer.cornerRadius = 4;
    SearchTextFiled.backgroundColor = [UIColor whiteColor];
    SearchTextFiled.delegate = self;
    [self.navigationItem setTitleView:SearchTextFiled];
    self.BaseTableView.delegate = self;
    self.BaseTableView.dataSource = self;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)searchBtnClick{
    searchText = SearchTextFiled.text;
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    AFJSONResponseSerializer * responseSerializer = [[AFJSONResponseSerializer alloc]init];
    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
    NSDictionary * parameter = @{@"i":@"0",@"size":@"7",@"searchText":searchText};
    NSLog(@"%@",parameter.allValues);
    [manager.requestSerializer setValue: appId forHTTPHeaderField:@"appId"];
    [manager POST:searchAlbum parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //先删除之前的所有数据
        [self.albumListDataArray removeAllObjects];
        //        NSLog(@"搜索到的声音%@",responseObject);
        NSArray * dataArray = [responseObject valueForKey:@"data"];
        NSLog(@"专辑总数%lu",(unsigned long)dataArray.count);
        for (int i = 0; i < dataArray.count; i++) {
            NSDictionary *dic = dataArray[i];
            AlbumListModel * model = [[AlbumListModel alloc] initWithDict:dic];
            [self.albumListDataArray addObject: model];
        }
        [self.BaseTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
    NSLog(@"搜索专辑");

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AlbumListTableViewCell * cell = [AlbumListTableViewCell CellWith:tableView];
    cell.AlbumCellTitle.text = self.albumListDataArray[indexPath.row].title;
    [cell.AlbumCellImgaView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",port,self.albumListDataArray[indexPath.row].imgUrl]]];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.albumListDataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AlbumDetailViewController * vc = [[AlbumDetailViewController alloc]init];
    vc.AlbumDtailModel = self.albumListDataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
